﻿using System;
using System.Diagnostics;
using My.Ioc;

namespace PropertyAndMethodInjection
{
    #region Test Types

    public class Teacher
    {
    }

    public class Grade
    {
        public Grade(Teacher teacher)
        {
            Teacher = teacher;
        }

        public Teacher Teacher { get; private set; }
        public int StudentCount { get; set; }
        public string TeacherName { get; set; }

        public void PrintRoomName(Room room)
        {
            Console.WriteLine(room.Name ?? "No room name given!");
        }
    }

    public class Room
    {
        public Grade Grade { get; set; }
        public string Name { get; set; }
    }

    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            IObjectContainer container = new ObjectContainer(false);
            Register(container);

            var grade = container.Resolve<Grade>();
            Debug.Assert(grade.TeacherName == "Johnny");
            Debug.Assert(grade.StudentCount == 50);

            var room = container.Resolve<Room>();
            Debug.Assert(room.Grade != null);

            Console.ReadLine();
        }

        static void Register(IObjectContainer container)
        {
            container.Register<Teacher>().In(Lifetime.Container());
            container.Register<Grade>()
                .WithPropertyValue("TeacherName", "Johnny")
                .WithPropertyValue("StudentCount", 50)
                .WithMethod("PrintRoomName")
                .In(Lifetime.Container());
            container.Register<Room>()
                .WithPropertyAutowired("Grade")
                .WithPropertyValue("Name", "RoomNameInjected")
                .In(Lifetime.Transient());
            container.CommitRegistrations();
        }
    }
}
